public class Sort {

    /**
     * 直接插入排序
     * 时间复杂度：O(N^2)
     * 时间复杂度：O(1)
     * 稳定性：稳定
     * @param array
     */
    public static void insert(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int j = i-1;
            int tmp = array[i];
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    array[j+1] = tmp;
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }

    /**
     * 希尔排序
     * 时间复杂度：O(N^1.3) - O(N^1.5)
     * 时间复杂度：O(1)
     * 稳定性：不稳定
     * @param array
     */
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            shell(array,gap);
            gap = gap / 2;
        }
        shell(array,gap);

    }
    public static void shell(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int j = i-gap;
            int tmp = array[i];
            for (; j >= 0; j-= gap) {
                if (array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    array[j+gap] = tmp;
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }
}
